package com.smartsoft.demo;

import android.app.Fragment;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.test.suitebuilder.annotation.LargeTest;
import android.test.suitebuilder.annotation.MediumTest;
import android.test.suitebuilder.annotation.SmallTest;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.smartsoft.demo.activity.VideoActivity;
import com.smartsoft.demo.fragment.ContactEditFragment;
import com.smartsoft.demo.fragment.ContactPreviewFragment;
import junit.framework.Assert;

import static android.os.SystemClock.sleep;

/**
 * Test video activity and fragments inside it
 */
public class VideoActivityTest extends ActivityInstrumentationTestCase2<VideoActivity> {

    VideoActivity activity;

    public VideoActivityTest() {
        super(VideoActivity.class);
    }

    @Override
    public void setUp() throws Exception {
        super.setUp();
        activity = getActivity();
        Log.i("1","start");
    }

    @Override
    public void tearDown() throws Exception {
        super.tearDown();
        if (activity != null) {
            activity.finish();
        }
        Log.i("1","down");
    }


    /**
     * Verifies that activity under test can be launched.
     */
    @SmallTest
    public void testSimpleCreateTest() {
        assertNotNull(activity);
    }

    @SmallTest
    public void testTitle() throws Exception {
        assertEquals("Title is wrong", "MainScreen", activity.getTitle());
    }

    /**
     * Test that edit fragment changes to preview fragment when user click on preview button
     */
    @SmallTest
    @UiThreadTest
    public void testGoPreview() {
        final VideoActivity a = this.activity;
        Button preview = (Button)(a.findViewById(R.id.goPreview));
        preview.performClick();
        activity.getFragmentManager().executePendingTransactions();
        a.getFragmentManager().executePendingTransactions();
        Fragment fragment = a.getFragmentManager().findFragmentById(R.id.fragmentPlace);
        assertEquals(fragment.getClass(), ContactPreviewFragment.class);
    }

    /**
     * Test data transfer between edit fragment and preview fragment
     */
    @MediumTest
    @UiThreadTest
    public void testDataTransferBetweenFragments() {
        final VideoActivity a = this.activity;

        ImageView avatar = (ImageView)(a.findViewById(R.id.imageView));

        TextView firstName = (TextView)(a.findViewById(R.id.firstName));
        firstName.setText("1111");
        TextView lastName = (TextView)(a.findViewById(R.id.lastName));
        lastName.setText("2222");
        TextView phone = (TextView)(a.findViewById(R.id.phone));
        phone.setText("3333");
        TextView email = (TextView)(a.findViewById(R.id.email));
        email.setText("4444");
        TextView skype = (TextView)(a.findViewById(R.id.skype));
        skype.setText("5555");
        Button preview = (Button)(a.findViewById(R.id.goPreview));

        preview.performClick();
        a.getFragmentManager().executePendingTransactions();

        firstName   = (TextView) (a.findViewById(R.id.firstName));
        assertEquals("1111", firstName.getText());
        lastName    = (TextView) (a.findViewById(R.id.lastName));
        assertEquals("2222", lastName.getText());
        phone       = (TextView) (a.findViewById(R.id.phone));
        assertEquals("3333", phone.getText());
        email       = (TextView) (a.findViewById(R.id.email));
        assertEquals("4444", email.getText());
        skype       = (TextView) (a.findViewById(R.id.skype));
        assertEquals("5555", skype.getText());
    }

    @LargeTest
    @UiThreadTest
    public void testDataSave() {
        final VideoActivity a = this.activity;

        testDataTransferBetweenFragments();

        Button backButton = (Button)(a.findViewById(R.id.backButton));
        backButton.performClick();
        a.getFragmentManager().executePendingTransactions();

        Fragment fragment = a.getFragmentManager().findFragmentById(R.id.fragmentPlace);
        assertEquals(fragment.getClass(), ContactEditFragment.class);


        EditText firstName = (EditText)(a.findViewById(R.id.firstName));
        assertEquals("1111", firstName.getText().toString());
        EditText lastName = (EditText)(a.findViewById(R.id.lastName));
        assertEquals("2222", lastName.getText().toString());
        EditText phone = (EditText)(a.findViewById(R.id.phone));
        assertEquals("3333", phone.getText().toString());
        EditText email = (EditText)(a.findViewById(R.id.email));
        assertEquals("4444", email.getText().toString());
        EditText skype = (EditText)(a.findViewById(R.id.skype));
        assertEquals("5555", skype.getText().toString());
    }



}

